﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Money
{
    public class TransactionComparer : IComparer<Transaction>
    {
        public enum SortField
        {
            Date,
            Account,
            Amount,
            Category,
            CategoryGroup,
            Type,
            Edited,
            Description,
            None
        }

        private SortField _sortField;
        private SortOrder _sortOrder;

        public TransactionComparer(SortField sortField, SortOrder sortOrder)
        {
            _sortField = sortField;
            _sortOrder = sortOrder;
        }

        public int Compare(Transaction x, Transaction y)
        {
            int returnValue = 1;

            switch (_sortField)
            {
                case SortField.Account:
                    if (_sortOrder == SortOrder.Ascending)
                    {
                        returnValue = x.Account.CompareTo(y.Account);
                    }
                    else
                    {
                        returnValue = y.Account.CompareTo(x.Account);
                    }
                    break;

                case SortField.Date:
                    if (_sortOrder == SortOrder.Ascending)
                    {
                        returnValue = x.Date.CompareTo(y.Date);
                    }
                    else
                    {
                        returnValue = y.Date.CompareTo(x.Date);
                    }
                    break;

                case SortField.Amount:
                    if (_sortOrder == SortOrder.Ascending)
                    {
                        returnValue = x.Amount.CompareTo(y.Amount);
                    }
                    else
                    {
                        returnValue = y.Amount.CompareTo(x.Amount);
                    }
                    break;

                case SortField.Category:
                    if (_sortOrder == SortOrder.Ascending)
                    {
                        returnValue = x.Category.CompareTo(y.Category);
                    }
                    else
                    {
                        returnValue = y.Category.CompareTo(x.Category);
                    }
                    break;

                case SortField.CategoryGroup:
                    if (_sortOrder == SortOrder.Ascending)
                    {
                        returnValue = x.CategoryGroup.CompareTo(y.CategoryGroup);
                    }
                    else
                    {
                        returnValue = y.CategoryGroup.CompareTo(x.CategoryGroup);
                    }
                    break;

                case SortField.Type:
                    if (_sortOrder == SortOrder.Ascending)
                    {
                        returnValue = x.Type.CompareTo(y.Type);
                    }
                    else
                    {
                        returnValue = y.Type.CompareTo(x.Type);
                    }
                    break;

                case SortField.Edited:
                    if (_sortOrder == SortOrder.Ascending)
                    {
                        returnValue = x.IsDirty.CompareTo(y.IsDirty);
                    }
                    else
                    {
                        returnValue = y.IsDirty.CompareTo(x.IsDirty);
                    }
                    break;

                case SortField.Description:
                    if (_sortOrder == SortOrder.Ascending)
                    {
                        returnValue = x.Description.CompareTo(y.Description);
                    }
                    else
                    {
                        returnValue = y.Description.CompareTo(x.Description);
                    }
                    break;

            }

            return returnValue;
        }
    }
}
